/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.ChiTietGioHang_pojo;
import pojo.KhachHang_pojo;

/**
 *
 * @author Rocker000
 */
@WebServlet(name = "giohang_controller", urlPatterns = {"/giohang_controller"})
public class giohang_controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            HttpSession session = request.getSession();
            String action = request.getParameter("action");
            if(action!=null)
            {
                //Xem gio hang 
                if(action.equals("view"))
                {
                    KhachHang_pojo kh = new KhachHang_pojo();
                    if(session.getAttribute("KhachHang")!=null)
                        kh = (KhachHang_pojo)session.getAttribute("KhachHang");
                    
                    ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
                    if(session.getAttribute("GioHang")!=null)
                    {
                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                    }
                                        
                    request.setAttribute("GioHang", ds);
                    session.setAttribute("Kh", kh);
                    request.getRequestDispatcher("/giohang.jsp").forward(request, response);
                }
                                
                //Them vao gio hang
                if(action.equals("themvaogiohang"))
                {                 
                    if(session.getAttribute("IsLogin")=="True")
                    {
                        String masanpham = request.getParameter("masanpham");    
                        ArrayList<ChiTietGioHang_pojo> ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                        int flag = 0;
                        for(int i=0;i< ds.size();i++)
                        {                    
                            if(ds.get(i).getMasanpham().equals(masanpham))
                            {
                                ds.get(i).setSoluong(ds.get(i).getSoluong()+1); 
                                flag = 1;                                 
                                request.setAttribute("err_giohang","Thêm thành công!");                               
                                response.sendRedirect("giohang_controller?action=view");
                            }                        
                        }
                        if(flag ==0)
                        {
                            KhachHang_pojo kh = (KhachHang_pojo)session.getAttribute("KhachHang");                
                            ChiTietGioHang_pojo chitiet = new ChiTietGioHang_pojo();  
                            chitiet.setMasanpham(masanpham);
                            chitiet.setSoluong(1);
                            chitiet.setMagiohang(kh.LayMaGioHang());

                            ds.add(chitiet);

                            session.setAttribute("GioHang", ds);
                            request.setAttribute("masanpham",masanpham);
                            request.setAttribute("err_giohang","Thêm thành công!");
                            response.sendRedirect("giohang_controller?action=view");                                                                                    
                        }        
                    }            
                    else
                    {
                        request.setAttribute("err","Vui lòng đăng nhập để sử dụng chức năng này!!"); 
                        out.println("helloword");
                        request.getRequestDispatcher("giohang_controller?action=view").forward(request, response);
                    }
                } 
                if(action.equals("xoachitiet"))
                {
                    String machitiet = request.getParameter("machitiet");
                    ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
                    if(session.getAttribute("GioHang")!=null)
                    {
                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                    }
                    for(int i =0;i<ds.size();i++)
                    {
                        if(String.valueOf(ds.get(i).getMachitiet()).equals(machitiet))
                            ds.remove(i);
                    }
                    session.setAttribute("GioHang", ds);
                    request.getRequestDispatcher("giohang_controller?action=view").forward(request, response);
                }
                if(action.equals("capnhatgiohang"))
                {
                    ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
                    if(session.getAttribute("GioHang")!=null)
                    {
                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                    }
                    String [] soluong = request.getParameterValues("txtsoluong");
                    for(int i = 0 ; i< soluong.length ; i++)
                    {     
                        try
                        {
                            int sl = Integer.parseInt(soluong[i]);
                            ds.get(i).setSoluong(sl); 
                        }
                        catch(Exception ex)
                        {
                            out.println(i+ex.getMessage());
                        }      
                    }     
                    request.setAttribute("err_giohang","Đã cập nhật!");
                    session.setAttribute("GioHang", ds);
                    request.getRequestDispatcher("giohang_controller?action=view").forward(request, response);
                }
                if(action.equals("muahet"))
                {
                     ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
                    if(session.getAttribute("GioHang")!=null)
                    {
                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                    }
                    
                    
                    session.setAttribute("GioHang", ds);
                    request.getRequestDispatcher("giohang_controller?action=view").forward(request, response);
                }
                if(action.equals("xoahet"))
                {
                    ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
                    if(session.getAttribute("GioHang")!=null)
                    {
                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                    }
                    for(int i = ds.size()-1;i>=0;i--)
                    {
                        ds.remove(i);
                    }
                    request.setAttribute("err_giohang","giỏ hàng của bạn đã được làm sạch");
                    session.setAttribute("GioHang", ds);
                    request.getRequestDispatcher("giohang_controller?action=view").forward(request, response);
                }
                if(action.equals("luugiohang"))
                {
                     KhachHang_pojo kh = new KhachHang_pojo();
                     if(session.getAttribute("KhachHang")!=null)
                        kh = (KhachHang_pojo)session.getAttribute("KhachHang");
                     //KhachHang_pojo k = (KhachHang_pojo)session.getAttribute("KhachHang");
                     kh.XoaToanBoChiTietGioHang();
                     ArrayList<ChiTietGioHang_pojo> giohang =(ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
                     kh.LuuGioHang(giohang); 
                     request.setAttribute("err_giohang","Lưu Thành Công!");
                     request.getRequestDispatcher("giohang_controller?action=view").forward(request, response);
                }
                
            }
//            if(request.getParameter("btxoa")!=null)
//                {                                        
//                    ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
//                    if(session.getAttribute("GioHang")!=null)
//                    {
//                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
//                    }
//                    
//                    String [] machitietbixoa = request.getParameterValues("cbxoa");           
//                    for(int i = 0; i< machitietbixoa.length;i++)
//                    {             
//                        String s = machitietbixoa[i];        
//                        for(int j = 0;j< ds.size();j++)
//                        {
//                            if(String.valueOf(ds.get(j).getMachitiet()).equals(s))
//                            {                
//                                ds.remove(j);            
//                                break;
//                            }  
//                        }        
//                    }                    
//                    session.setAttribute("GioHang", ds); 
//                    request.getRequestDispatcher("GioHangController?Action=View").forward(request, response);                    
//                }
            
//            if(request.getParameter("btcapnhat")!=null)
//                {
//                    ArrayList<ChiTietGioHang_pojo> ds = new  ArrayList<ChiTietGioHang_pojo>();
//                    if(session.getAttribute("GioHang")!=null)
//                    {
//                        ds = (ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
//                    }
//                    String [] soluong = request.getParameterValues("tbsoluong");
//                    for(int i = 0 ; i< soluong.length ; i++)
//                    {     
//                        try
//                        {
//                            int sl = Integer.parseInt(soluong[i]);
//                            ds.get(i).setSoluong(sl); 
//                        }
//                        catch(Exception ex)
//                        {
//                            out.println(i+ex.getMessage());
//                        }      
//                    }                    
//                    session.setAttribute("GioHang", ds);
//                    request.getRequestDispatcher("GioHangController?Action=View").forward(request, response);
//                }
            
//            if(request.getParameter("btluugiohang")!=null)
//                {      
//                    KhachHang_pojo kh = new KhachHang_pojo();
//                    if(session.getAttribute("KhachHang")!=null)
//                        kh = (KhachHang_pojo)session.getAttribute("KhachHang");
//                     //KhachHang_pojo k = (KhachHang_pojo)session.getAttribute("KhachHang");
//                     kh.XoaToanBoChiTietGioHang();
//                     ArrayList<ChiTietGioHang_pojo> giohang =(ArrayList<ChiTietGioHang_pojo>)session.getAttribute("GioHang");
//                     kh.LuuGioHang(giohang); 
//                     request.setAttribute("err","Lưu Thành Công!");
//                     request.getRequestDispatcher("GioHangController?Action=View").forward(request, response);
//                }
            
        } 
        finally 
        {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
